#include <iostream>
#include <cstdio>
#include <string>
#include <unistd.h>
#include <pthread.h>

//__thread int gval = 100;
int gval = 100;

std::string ToHex(pthread_t tid)
{
    char buffer[128]={0};
    snprintf(buffer, sizeof(buffer),"0x%lx", tid);
    return buffer;
}

void* threadrun(void* args)
{
    std::string* name = static_cast<std::string*>(args);
    while(true)
    {
        std::string&& id = ToHex(pthread_self());
        std::cout<<name<<" is running, tid: "<<id<<"  gval: "<<gval<<", &gval: "<<&gval<<std::endl;
        sleep(1);
        gval++;
    }
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, threadrun, (void*)"thread-1");

    while(true)
    {
        sleep(1);
        std::cout<<"main thread, gval: "<<gval<<", &gval: "<<&gval<<std::endl;
    }

    return 0;
}
